<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
  entry ||= nil
  entry_exists = entry && entry ? true : false
  out_of_context ||= false
  entry_context ||= entry && entry.context ? entry.context : @context
  entry_id = entry ? entry.id : "{{ id }}"
  user_id = entry.try_rescue(:user_id) || "{{ user_id }}"
  topic ||= nil
  discussion_topic_id = entry.try_rescue(:discussion_topic_id) || (topic && topic.id) || "{{ discussion_topic_id }}"
  entry_key = entry.new_record? ? entry.try_rescue(:discussion_topic).try_rescue(:cache_key) : entry.try_rescue(:cache_key) if entry
  entry_key ||= entry_context.try_rescue(:asset_string) || 'blank'
  skip_sub_entries ||= false
  read_state = entry ? entry.read_state(@current_user) : "read"
  link_to_headless ||= false
  headless_param = {:headless => 1, :hide_student_names => hide_student_names, :student_id => user_id} if link_to_headless
  hide_student_names = hide_student_names
 %>
<% cache([
    'entry_message_render_2_',
    entry_key,
    out_of_context,
    skip_sub_entries,
    @topic_agglomerated,
    Time.zone.utc_offset,
    read_state,
    link_to_headless
  ].join('/')) do %>
<div class="discussion_entry communication_message can_be_marked_as_read <%= read_state %>" <%= hidden(true) unless entry_exists %> id="entry_<%= entry_exists ? entry.id : "blank" %>" data-mark-read-url="<%= entry_exists && entry_id && context_url(entry_context, :api_v1_context_discussion_topic_discussion_entry_mark_read_url, discussion_topic_id, entry_id) %>">
  <div class="header clearfix">
    <% if entry && entry.new_record? && entry.new_record_header %>
      <%= entry.new_record_header %>
    <% else %>
      <div class="header_title">
        <% if out_of_context %>
          <span style="font-size: 0.8em; padding-left: 20px;">from
            <% if assignment_visible_to_user %>
              <a href="<%= context_url(entry_context, :context_discussion_topic_url, (entry ? entry.discussion_topic_id : '{{ topic_id }}'), headless_param) %>" style="font-size: 1.2em; font-weight: bold;"><%= entry.discussion_topic.title %></a>
            <% else %>
              <b> <%= entry.discussion_topic.title %> </b>
            <% end %>
          </span>
        <% else %>
          <% if @topic_agglomerated && entry %>
            <a href="<%= context_url(entry_context, :context_discussion_topic_url, entry ? entry.discussion_topic_id : '{{ topic_id }}') %>" style="font-weight: bold;"><%= entry.context.name %></a>
          <% end %>
          <a title="<%= t :authors_name, "Author's name" %>" href="<%= context_url(entry_context, :context_user_url, user_id) %>" class="user_name"><%= context_user_name(@context, entry && entry.user_id) %></a>
        <% end %>
      </div>
      <div class="teaser_message"></div>
      <% unless out_of_context %>
      <div class="link_box">
        <span class="links">
          <% if !@topic_agglomerated && (!entry || !entry.new_record?) %>
            <a href="<%= context_url(entry_context, :new_context_discussion_entry_url) %>" class="add_entry_link disabled_link atr-reply" title="<%= t :reply_to_message, "Make a Side Comment" %>"><%= t :reply_to_message, "Make a Side Comment" %></a>
            <a href="<%= context_url(entry_context, :edit_context_discussion_entry_url, entry_id) %>" class="edit_entry_link disabled_link atr-edit" title="<%= t :edit_message, "Edit this Message" %>"><%= t :edit_message, "Edit this Message" %></a>
            <a href="<%= context_url(entry_context, :context_discussion_entry_url, entry_id) %>" class="delete_entry_link disabled_link atr-delete" title="<%= t :delete_message, "Delete this Message" %>"><%= t :delete_message, "Delete this Message" %></a>
          <% end %>
        </span>
      </div>
      <% end %>
      <div class="post_date time_ago_date" data-timestamp="<%= entry.try_rescue(:created_at).try_rescue(:iso8601) %>"><%= datetime_string(entry.try_rescue(:created_at)) || nbsp %></div>
      <div class="clear"></div>
    <% end %>
  </div>
  <div class="content">
    <% unless hide_student_names %>
      <div style="float: left; margin: 0 5px 0 -10px;"><%= avatar((entry.try_rescue(:user_id)), context_code: entry_context.asset_string) %></div>
    <% end %>
    <div class="course_id" style="display: none;"><%= entry_context.id if entry_context && entry_context.is_a?(Course) %></div>
    <div class="message user_content"><%= user_content(entry.try_rescue(:message)) || nbsp %></div>
    <textarea class="message_html" style="display: none;"><%= h(entry.try_rescue(:message)) %></textarea>
    <div class="link_box">
      <% if entry && entry.attachment %>
        <div class="attachment_data" style="float: left;">
          <%= before_label :attached_file, "Attached File" %> <a href="<%= file_download_url(entry.attachment, :verifier => entry.attachment.uuid) %>" class="attachment_name"><%= entry.attachment.try_rescue(:display_name) || nbsp %></a>
        </div>
      <% end %>
      <div class="clear"></div>
    </div>
    <div class="subcontent" style="<%= hidden if entry && entry.new_record? %>">
      <% entries ||= []; @grouped_entries ||= {} %>
      <% if entry && !skip_sub_entries %>
        <% entries = @grouped_entries[entry.id] %>
        <% entries ||= [] #entry.discussion_subentries.active %>
        <% if entries.length > 3 %>
          <div class="communication_sub_message">
            <div class="content behavior_content">
              <a href="#" class="show_sub_messages_link"><%= t :show_more_entries, { :one => "Show 1 More Entry", :other => "Show %{count} More Entries" }, :count => (entries.length - 2) %></a>
            </div>
          </div>
        <% end %>
        <% entries.each_with_index do |entry, idx| %>
          {{> views/discussion_topics/sub_entry}}
        <% end %>
      <% end %>
      <% if entry && entry.editor_id && entry.editor_id != entry.user_id %>
        <div style="font-size: 0.8em;">
          <%= t :edited_comment, "This comment was edited by %{user}.", :user => link_to(context_user_name(entry_context, entry.editor_id), context_url(entry_context, :context_user_url, entry.editor_id)) %>
        </div>
      <% end %>
      <div class="communication_sub_message reply_message <%= 'lonely_behavior_message' if entries.empty? %>" style="display: none;">
        <div class="content behavior_content">
          <a href="#" class="add_entry_link textarea"><%= t :make_side_comment, "Make a Side Comment..." %></a>
        </div>
      </div>
      {{> views/discussion_topics/sub_entry}}
    </div>
  </div>
  <div style="display: none;">
    <% if !@topic_agglomerated && (!entry || !entry.new_record?) %>
    <a href="<%= context_url(entry_context, :context_discussion_entry_url, entry_id) %>" class="edit_entry_url">&nbsp;</a>
    <% end %>
    <span class="parent_id"><%= entry.parent_id rescue nbsp %></span>
    <span class="id"><%= entry ? entry.id : nbsp %></span>
  </div>
</div>
<div class="discussion_subtopic entry_list" id="replies_entry_<%= entry_exists ? entry.id : "blank" %>" style="display: none;">
</div>
<% end %>
